$(document).ready(function() {
    var f = document.getElementById('tahun_filter');
    var thn = f.options[f.selectedIndex].value;

    $('#tahun_filter').on('change',function(e){
        var e = document.getElementById('tahun_filter');
        var tahun_filter = e.options[e.selectedIndex].value;
        loadAll(tahun_filter);

    });

    loadAll(thn);
    $("#btn_simpan").click(function() {
        $.post('controller/cont.libur.php', $("#frm_libur").serialize(), function(data) {
            if (data.msg) {
                if (data.edit) {
                    goAdd();
                    document.getElementById("tgl_libur").value = data.libur.tgl;
                    document.getElementById("uraian").value = data.libur.uraian;

                    var ddl = document.getElementById('idHari');
                    var opts = ddl.options.length;
                    for (var i = 0; i < opts; i++) {
                        if (ddl.options[i].value == data.libur.idHari) {
                            ddl.options[i].selected = true;
                            break;
                        }
                    }

                    document.getElementById("frm_aksi").value = "simpan_edit";
                    document.getElementById("saveButton").className = "buttonshort";
                    document.getElementById("undoButton").className = "buttonshort";
                    document.getElementById("hapusButton").className = "buttonshortdis";
                    document.getElementById("editButton").className = "buttonshortdis";
                } else {
                    document.getElementById("sukses").innerHTML = data.sukses;
                    $("#div_sukses").fadeIn(500).delay(3500).fadeOut(500);
                    goUndo();
                    loadAll(thn);
                }
            } else {
                document.getElementById("error").innerHTML = data.eror;
                $("#div_error").fadeIn(500).delay(3500).fadeOut(500);
                goUndo();
                //loadAll();
            }
        }, 'json');

        return false;
    });



    return false;
});

function loadAll(thn) {
    $.getJSON('controller/cont.libur.php', {aksi_get_libur: true, tahun: thn}, function(data) {
        $('#list_libur tbody').html('');
        var tab = '';
        if (data.msg) {
            $.each(data.libur, function(index, libur) {
                tab += "<tr>";
                tab += "<td>" + libur.tgl + "</td>";
                tab += "<td>" + libur.uraian + "</td>";
                tab += "<td>" + libur.tahun + "</td>";
                tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\"" + libur.id + "\"/></td>";
                tab += "</tr>";
            });

        } else {
            tab += "<tr>"
            tab += "<td colspan=\"5\">Data Tidak Ada</td>";
            tab += "</tr>";
        }
        $('#list_libur tbody').append(tab);
    });
}